#include "StdAfx.h"
#include ".\checkbox.h"

// class CJoyCheckGroup

//##ModelId=44CF17B901B6
CJoyCheckGroup::CJoyCheckGroup()
{
    m_autoCheck = false;
    m_autoUncheck = false;
}

//##ModelId=44CF17B901B7
CJoyCheckGroup::~CJoyCheckGroup()
{
}

//##ModelId=44CF17B901B9
void CJoyCheckGroup::Add(CJoyCheckBox* box)
{
    box->m_autoUncheck  = m_autoUncheck;
    if(m_autoCheck)
    {
        box->SetCheck(m_boxs.GetCount() == 0);
    }

    m_boxs.AddTail(box);
    box->m_group = this;
}

//##ModelId=44CF17B901C8
CJoyCheckBox* CJoyCheckGroup::GetSel()
{
    POSITION pos = m_boxs.GetHeadPosition();
    while(pos != NULL)
    {
        CJoyCheckBox* box = m_boxs.GetNext(pos);
        if(box->IsChecked())
        {
            return box;
        }
    }
    return NULL;
}

//##ModelId=44CF17B901C7
void CJoyCheckGroup::CheckFirstVisible()
{
    POSITION pos = m_boxs.GetHeadPosition();
    while(pos != NULL)
    {
        CJoyCheckBox* box = m_boxs.GetNext(pos);
        if(box->IsVisible())
        {
            box->SetCheck(true);
        }
    }
}

//##ModelId=44CF17B901BB
void CJoyCheckGroup::Remove(CJoyCheckBox* box)
{
    POSITION pos = m_boxs.GetHeadPosition();
    while(pos != NULL)
    {
        POSITION tempPos = pos;
        CJoyCheckBox* p = m_boxs.GetNext(pos);
        if(!m_autoCheck)
        {
            return;
        }
        if(p == box)
        {
            m_boxs.RemoveAt(tempPos);
            box->m_group = NULL;
            if(box->m_checked)
            {
                CheckFirstVisible();
            }
            return;
        }
    }
}

//##ModelId=44CF17B901C9
void CJoyCheckGroup::ShowAll(bool show)
{
    POSITION pos = m_boxs.GetHeadPosition();
    while(pos != NULL)
    {
        CJoyCheckBox* p = m_boxs.GetNext(pos);
        p->SetVisible(show);
    }
}

//##ModelId=44CF17B901C5
void CJoyCheckGroup::Check(CJoyCheckBox* box)
{
    POSITION pos = m_boxs.GetHeadPosition();
    while(pos != NULL)
    {
        CJoyCheckBox* p = m_boxs.GetNext(pos);
        p->SetCheck(p == box);
    }
}


// class CJoyCheckBox

//##ModelId=44CF17B90222
CJoyCheckBox::CJoyCheckBox(void)
{
    m_btnDown   = false;
    m_checked   = false;
    m_autoUncheck = true;
    m_leftDownCheck = false;
    m_group     = NULL;
    m_onCheck   = NULL;
    m_eventCtrl = NULL;

    m_skinArea  = true;
}

//##ModelId=44CF17B90224
CJoyCheckBox::~CJoyCheckBox(void)
{
}

//##ModelId=44CF17B9022B
void CJoyCheckBox::SetCheck(bool checked)
{
    if(m_checked == checked)
    {
        return;
    }
    if(m_eventCtrl != NULL)
    {
        (m_eventCtrl->*m_onCheck)(this, checked);
    }
    if(m_checked == checked)
    {
        return;
    }
    m_checked = checked;
    if(m_checked)
    {
        if(m_group != NULL)
        {
            m_group->Check(this);
        }
    }
    InvalidateCtrl();
}

//##ModelId=44CF17B90232
void CJoyCheckBox::SetCheckEvent(CJoyWnd* ctrl, PCheckEvent onCheck)
{
    m_eventCtrl = ctrl;
    m_onCheck   = onCheck;
}

//##ModelId=44CF17B90235
void CJoyCheckBox::SetImage(PCTSTR pszImage)
{
    m_image = JoyGetManager()->CreateImage(pszImage);
}

//##ModelId=44CF17B90203
void CJoyCheckBox::CM_LoadSkin(CJoyXmlElement* pSkinElem)
{
    __super::CM_LoadSkin(pSkinElem);

    CJoyXmlAttribute* pImageAttrib = pSkinElem->FindAttribute("image");
    if(pImageAttrib != NULL)
    {
        if(!pImageAttrib->m_value.IsEmpty())
        {
            SetImage(pImageAttrib->m_value);
        }
    }
}

//##ModelId=44CF17B90206
void CJoyCheckBox::CM_MouseHover()
{
    m_btnDown  = false;
}

//##ModelId=44CF17B90217
void CJoyCheckBox::CM_Destroy()
{
    if(m_group != NULL)
    {
        m_group->Remove(this);
    }
	__super::CM_Destroy();
}

//##ModelId=44CF17B90208
void CJoyCheckBox::CM_Mouse(MouseMessage msg, const CPoint& pt)
{
    __super::CM_Mouse(msg, pt);

    if(!m_enabled)
    {
        return;
    }
    bool doCheck = false;
    switch(msg)
    {
    case MM_LeftDown:
        m_btnDown  = true;
        doCheck = m_leftDownCheck;
    case MM_LeftUp:
        if(m_btnDown)
        {
            m_btnDown = false;
            if(m_leftDownCheck)
            {
                break;
            }
            doCheck = true;
        }        
        break;
    default:
        break;
    }
    if(doCheck)
    {
        if(m_autoUncheck)
        {
            SetCheck(!m_checked);
        }
        else
        {
            if(!m_checked)
            {
                SetCheck(true);
            }
        }
    }
}

//##ModelId=44CF17B90214
void CJoyCheckBox::CM_Draw(Graphics& graph)
{
    if(m_image == NULL)
    {
        return;
    }
    int i = m_checked ? 1 : 0;

    INT h = (INT)m_image->GetHeight();
    INT w = (INT)m_image->GetWidth()/2;
    INT l = w*i;

    Rect c(0, 0, w, h);

    graph.DrawImage(m_image, c, l, 0, w, h, UnitPixel, NULL, NULL, NULL);
}
